import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/layout'
import video from '@/router/modules/video'
import approvalsettings from './modules/approvalsettings'
Vue.use(Router)
// 静态路由
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true // 是否隐藏组件，在SidebarItem这个组件中判断的
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard', // 二级路由地址为空时 表示 / 显示 一级路由 + 二级路由
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'dashboard' }
      }
    ]
  },
  {
    path: '/process',
    name: 'Process',
    component: Layout,
    children: [
      {
        path: 'definition',
        name: 'Definition',
        component: () => import('@/views/processDefinition/index')
      }
    ]
  },
  video,
  approvalsettings
  // 页面兜底，都找不到的情况下去404,404页面必须放在最后,如果我们在动态路由添加了404页面，那么这个页面就不需要了
  // { path: '*', redirect: '/404', hidden: true }
]
// 新版本呢router3.1.0+以上的版本，router.push()方法返回一个promise，如果路由跳转失败，会抛出一个错误，,这里全局修改一下
const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes // 默认引入的是我们的静态路由
  })

const router = createRouter()

// 退出登录重置路由信息
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export default router
